package racing.car;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * A Factory class for creating the RacingCar.
 * 
 * @author Sarathit Sangtaweep 5510546182
 * @author Veerapat Teeravipark 5510547022
 * @version 2013
 */
public class RacingCarFactory {
	/**All car.*/
	private static Map<String, Integer[]> mapCar;
	static {
		mapCar = new HashMap<String, Integer[]>();
		mapCar.put("1JaVaCar", new Integer[] { 3, 7, 6, 3 });
		mapCar.put("2Speedy", new Integer[] { 4, 5, 5, 2 });
		mapCar.put("3Destiny", new Integer[] { 1, 11, 6, 5 });
		mapCar.put("4Equalitiez", new Integer[] { 3, 6, 3, 3 });
	}
	/**Factory object.*/
	private static RacingCarFactory instance = new RacingCarFactory();

	/**
	 * Constructor of the class.
	 */
	protected RacingCarFactory() {
	}

	/**
	 * Get the object of the factory class.
	 * @return the object of the factory class.
	 */
	public static RacingCarFactory getInstance() {
		return instance;
	}

	/**
	 * Create the RacingCar from the name.
	 * @param name the name of the RacingCar.
	 * @return the created RacingCar with the name.
	 */
	public RacingCar createCar(String name) {
		return new RacingCar(name,mapCar.get(name));
	}
	
	/**
	 * Get the array of all car's name.
	 * @return the array of all car's name.
	 */
	public String[] getAllCar(){
		Iterator<String> iter = mapCar.keySet().iterator();
		List<String> list = new ArrayList<String>();
		while(iter.hasNext()){
			list.add((String) iter.next());
		}
		Collections.sort(list);
		String[] allCar = new String[mapCar.size()];
		list.toArray(allCar);
		return allCar;
	}

}
